package com.tqjc.execute.work.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tqjc.execute.utils.HttpPLCUtils;
import com.tqjc.execute.utils.PublicResult;
import com.tqjc.execute.work.CraneWorkFactory;
import com.tqjc.execute.work.service.WorkService;
import com.tqjc.system.core.annotation.Log;
import com.tqjc.system.core.model.LogType;
import com.tqjc.system.core.util.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Author: 86195
 * @CreateTime: 2024-05-28  17:36
 * @Description: TODO
 * @Version: 1.0
 */
@Controller
@RequestMapping(path = "/work")
@Slf4j
public class WorkPlusController {

    @Resource
    WorkService workService;

    @Resource
    CraneWorkFactory craneWorkFactory;

    @Value("${server_host}")
    private String serverHost;

    @Autowired
    private RestTemplate restTemplate;



    @RequestMapping("/move1Plus")
    @ResponseBody
    @Log(type = LogType.work, name = "自由移动辊道", memo = "半自动任务自由移动到辊道")
    public PublicResult move1Plus(@RequestParam Map<String, String> params) {
        String craneType = params.get("craneType").replace("\"", "");


        try {
            log.info("用户{}点击了{}天车半自动任务自由移动辊道按钮开始",ShiroUtils.getSessionUserName(),craneType);

            JSONObject inputData = JSON.parseObject(params.get("inputData"));

            Integer sheetNumber = inputData.getInteger("sheetNumber");

            boolean checkFlag = this.checkMode(craneType);
            if(checkFlag){
                return PublicResult.fail("当前天车模式非远程半自动模式");
            }

            PublicResult result2 = workService.checkSemiAutoIsReadyWork(craneType);  //是否正在执行子任务
            if (!result2.isSuccess()) {
                return result2;
            }

//            PublicResult result3 = craneWorkFactory.getCraneWorkImpl(craneType).getData().checkIsSuckedSteel(); //在停电情况下，如果恢复电后，判断天车是否吸板，如果是吸板，就不让启动。
//            if (result3.isSuccess()) {
//                return PublicResult.fail("吊具有板，不能执行此步骤");
//            }


            //清中止信号
            HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.8.5",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","任务中止",false);
            if("11C南".equals(craneType) || "11C北".equals(craneType)){

                String wideSteelFlag = inputData.getString("wideSteelInfo");
                log.info("用户{}点击了{}天车半自动任务自由移动辊道按钮开始,超宽板选择信号为：{}",ShiroUtils.getSessionUserName(),craneType,wideSteelFlag);
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"M1999.0",HttpPLCUtils.dataTypeList.get("bool"),wideSteelFlag,"中控超宽板选择",false);
            }

            PublicResult result4=craneWorkFactory.getCraneWorkImpl(craneType).getData().doAction(1);  // 半自动任务只能一次运输一块板

            log.info("用户{}点击了{}天车半自动任务自由移动辊道按钮结束",ShiroUtils.getSessionUserName(),craneType);

            return PublicResult.ok();
        }catch (Exception ex){
            log.error("用户{}点击了{}天车半自动任务自由移动辊道按钮异常",ShiroUtils.getSessionUserName(),craneType,ex);
            return PublicResult.fail(ex.getMessage());
        }

    }


    //模式为远程自动（5）时，返回false
    private boolean checkMode(String craneType) {
        try {

            String mode= HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"模式",false);
            if("6".equals(mode)){
                return false;
            }
        } catch (Exception e){
            log.error("检查天车模式时，读取对应天车模式点位信息异常");

        }

        return true;
    }


    @RequestMapping("/suck2Plus")
    @ResponseBody
    @Log(type = LogType.work, name = "自由吸板", memo = "半自动任务自由吸板")
    public PublicResult suck2Plus(@RequestParam Map<String, String> params) {
        String craneType = params.get("craneType").replace("\"", "");
        try {
            log.info("用户{}点击了{}天车半自动任务自由吸板按钮开始",ShiroUtils.getSessionUserName(),craneType);

            JSONObject inputData = JSON.parseObject(params.get("inputData"));

            Integer sheetNumber = inputData.getInteger("sheetNumber");
            boolean checkFlag = this.checkMode(craneType);
            if(checkFlag){
                return PublicResult.fail("当前天车模式非远程半自动模式");
            }

            PublicResult result2 = workService.checkSemiAutoIsReadyWork(craneType);  //是否正在执行子任务
            if (!result2.isSuccess()) {
                return result2;
            }

//            PublicResult result3 = craneWorkFactory.getCraneWorkImpl(craneType).getData().checkIsSuckedSteel(); //在停电情况下，如果恢复电后，判断天车是否吸板，如果是吸板，就不让启动。
//            if (result3.isSuccess()) {
//                return PublicResult.fail("吊具有板，不能执行此步骤");
//            }


            //清中止信号
            HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.8.5",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","任务中止",false);

            PublicResult result4=craneWorkFactory.getCraneWorkImpl(craneType).getData().doAction(2);  // 半自动任务只能一次运输一块板

            log.info("用户{}点击了{}天车半自动任务自由吸板按钮结束",ShiroUtils.getSessionUserName(),craneType);

            return PublicResult.ok();
        }catch (Exception ex){
            log.error("用户{}点击了{}天车半自动任务自由吸板按钮异常",ShiroUtils.getSessionUserName(),craneType,ex);
            return PublicResult.fail(ex.getMessage());
        }

    }


    @RequestMapping("/move3Plus")
    @ResponseBody
    @Log(type = LogType.work, name = "自由移动到垛位", memo = "半自动任务自由移动到垛位")
    public PublicResult move3Plus(@RequestParam Map<String, String> params) {
        String craneType = params.get("craneType").replace("\"", "");
        try {
            log.info("用户{}点击了{}天车半自动任务自由移动到垛位按钮开始",ShiroUtils.getSessionUserName(),craneType);

            JSONObject inputData = JSON.parseObject(params.get("inputData"));

            Integer sheetNumber = inputData.getInteger("sheetNumber");
            boolean checkFlag = this.checkMode(craneType);
            if(checkFlag){
                return PublicResult.fail("当前天车模式非远程半自动模式");
            }

            PublicResult result2 = workService.checkSemiAutoIsReadyWork(craneType);  //是否正在执行子任务
            if (!result2.isSuccess()) {
                return result2;
            }

//            PublicResult result3 = craneWorkFactory.getCraneWorkImpl(craneType).getData().checkIsSuckedSteel(); //在停电情况下，如果恢复电后，判断天车是否吸板，如果是吸板，就不让启动。
//            if (result3.isSuccess()) {
//                return PublicResult.fail("吊具有板，不能执行此步骤");
//            }
            Integer stockNo = null;
            if("11C南".equals(craneType)||"11C北".equals(craneType)){
                stockNo = inputData.getInteger("stock");
                craneWorkFactory.getCraneWorkImpl(craneType).getData().setStockNo(stockNo+"");
            }
            //清中止信号
            HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.8.5",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","任务中止",false);
            if("11C南".equals(craneType) || "11C北".equals(craneType)){
                String wideSteelFlag = inputData.getString("wideSteelInfo");
                log.info("用户{}点击了{}天车半自动任务自由移动辊道按钮开始,超宽板选择信号为：{}",ShiroUtils.getSessionUserName(),craneType,wideSteelFlag);
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"M1999.0",HttpPLCUtils.dataTypeList.get("bool"),wideSteelFlag,"中控超宽板选择",false);
            }
            PublicResult result4=craneWorkFactory.getCraneWorkImpl(craneType).getData().doAction(3);  // 半自动任务只能一次运输一块板

            log.info("用户{}点击了{}天车半自动任务自由移动到垛位按钮结束",ShiroUtils.getSessionUserName(),craneType);

            return PublicResult.ok();
        }catch (Exception ex){
            log.error("用户{}点击了{}天车半自动任务自由移动到垛位按钮异常",ShiroUtils.getSessionUserName(),craneType,ex);
            return PublicResult.fail(ex.getMessage());
        }

    }


    @RequestMapping("/place4Plus")
    @ResponseBody
    @Log(type = LogType.work, name = "自由放板", memo = "半自动任务自由放板")
    public PublicResult place4Plus(@RequestParam Map<String, String> params) {
        String craneType = params.get("craneType").replace("\"", "");
        try {
            log.info("用户{}点击了{}天车半自动任务自由吸板按钮开始",ShiroUtils.getSessionUserName(),craneType);

            JSONObject inputData = JSON.parseObject(params.get("inputData"));

            Integer sheetNumber = inputData.getInteger("sheetNumber");
            boolean checkFlag = this.checkMode(craneType);
            if(checkFlag){
                return PublicResult.fail("当前天车模式非远程半自动模式");
            }

            PublicResult result2 = workService.checkSemiAutoIsReadyWork(craneType);  //是否正在执行子任务
            if (!result2.isSuccess()) {
                return result2;
            }

            //清中止信号
            HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.8.5",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","任务中止",false);

            PublicResult result4=craneWorkFactory.getCraneWorkImpl(craneType).getData().doAction(3);  // 半自动任务只能一次运输一块板

            log.info("用户{}点击了{}天车半自动任务自由吸板按钮结束",ShiroUtils.getSessionUserName(),craneType);

            return PublicResult.ok();
        }catch (Exception ex){
            log.error("用户{}点击了{}天车半自动任务自由吸板按钮异常",ShiroUtils.getSessionUserName(),craneType,ex);
            return PublicResult.fail(ex.getMessage());
        }

    }

}
